package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.Export;
import cn.itcast.domain.cargo.Packing;
import cn.itcast.domain.cargo.ShippingOrder;
import cn.itcast.domain.cargo.ShippingOrderExample;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.service.cargo.PackingService;
import cn.itcast.service.cargo.ShippingOrderService;
import cn.itcast.web.controller.BaseController;
import cn.itcast.web.utils.BeanMapUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import net.sf.jasperreports.engine.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/cargo/shipping")
public class ShippingOrderController extends BaseController {
    @Reference
    private ShippingOrderService shippingOrderService;

    @Reference
    private PackingService packingService;

    @Reference
    private ExportService exportService;
    /*
    作用：列表
    url：/cargo/shipping/list.do
    参数：
    返回值：cargo/shipping/shipping-list.jsp
     */
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "5") Integer pageSize) {

        ShippingOrderExample shippingOrderExample = new ShippingOrderExample();
        shippingOrderExample.createCriteria().andCompanyIdEqualTo(getLoginUserCompanyId());
        PageInfo<ShippingOrder> pageInfo = shippingOrderService.findByPage(shippingOrderExample, pageNum, pageSize);
        request.setAttribute("pageInfo", pageInfo);
        return "cargo/shipping/shipping-list";

    }

    /*
       url:${ctx}/cargo/shipping/delete.do?packingListId="+id
       作用：删除装箱单
       参数：装箱单id
    */
    @RequestMapping("/delete")
    @ResponseBody
    public String delete(String shippingOrderId){
        ShippingOrder shippingOrder = shippingOrderService.findById(shippingOrderId);
        if (shippingOrder.getState() == 0) {
            shippingOrderService.delete(shippingOrderId);
            return "true";
        } else {
            return "false";
        }
    }

    /*判断是否可以生成委托单*/
    @RequestMapping("/toShipping")
    @ResponseBody
    public String toShipping(String packingListId) {
        Packing packing = packingService.findById(packingListId);
        if (packing.getState() == 0) {
            return "true";
        } else {
            return "false";
        }
    }

    /*去到添加委托单页面*/
    @RequestMapping("/toAdd")
    public String toAdd(String packingListId) {
        Packing packing = packingService.findById(packingListId);

        String exportIds = packing.getExportIds();
        String[] exportIdList = exportIds.split(",");

        String lcNo = "";
        String marks = "";

        String shipmentPort = "";
        String destinationPort = "";

        for (int i = 0; i < exportIdList.length; i++) {
            String exportId = exportIdList[i];
            Export export = exportService.findById(exportId);

            lcNo =lcNo + export.getLcno() + " ";
            marks =marks + export.getMarks() + " ";

            shipmentPort = export.getShipmentPort();
            destinationPort = export.getDestinationPort();
        }
        ShippingOrder shippingOrder = new ShippingOrder();

        shippingOrder.setLcNo(lcNo);
        shippingOrder.setMarks(marks);
        shippingOrder.setPortOfLoading(shipmentPort);
        shippingOrder.setPortOfDischar(destinationPort);

        request.setAttribute("shippingOrder",shippingOrder);
        request.setAttribute("packingListId",packingListId);
        return "cargo/shipping/packing-toShipping";
    }


    /*保存委托单数据*/
    /*
       url:/cargo/shipping/save.do
       作用：保存，新增装箱单
       参数：packing对象
    */
    @RequestMapping("/save")
    public String save(ShippingOrder shippingOrder,String packingListId){
        //补全数据
        shippingOrder.setCompanyName(getLoginUserCompanyName());
        shippingOrder.setCompanyId(getLoginUserCompanyId());
        shippingOrder.setCreateBy(getLoginUser().getId());
        shippingOrder.setCreateDept(getLoginUser().getDeptId());
        shippingOrder.setShippingOrderId(packingListId);
        shippingOrder.setState(0);

        shippingOrderService.save(shippingOrder);

        Packing packing = packingService.findById(packingListId);
        packing.setState((long) 1);
        packingService.update(packing);

        return "redirect:/cargo/shipping/list.do";
    }

    @RequestMapping("/shippingPdf")
    @ResponseBody
    public void shippingPdf(String shippingOrderId) throws JRException, IOException {
        //通知浏览器以附件的形式下载
        response.setHeader("content-disposition","attachment;filename=shipping.pdf");

        //1. 得到模板的输入流
        InputStream inputStream = session.getServletContext().getResourceAsStream("/jasper/shipping.jasper");

        ShippingOrder shippingOrder = shippingOrderService.findById(shippingOrderId);

        Map<String, Object> map = BeanMapUtils.beanToMap(shippingOrder);
        map.put("protOfDischar",shippingOrder.getPortOfDischar());
        map.put("invoiceId", "59b1f72c-8c98-561871f9ad77");
        map.put("specialConditio","船运");
        map.put("tel","15602275130");
        map.put("fax","020-75130");

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        String data = simpleDateFormat.format(new Date());
        map.put("createTime",data);


        JasperPrint jasperPrint = JasperFillManager.fillReport(inputStream,map,new JREmptyDataSource());


        //3. 把pdf文件输出
        /*
            exportReportToPdfStream(JasperPrint jasperPrint, OutputStream outputStream)
                    jasperPrint： jasperprint的对象
                    outputStream: 输出的目标地址的输出流对象
         */
        JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());

    }
}
